package com.ulook.planetmanager.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.qmuiteam.qmui.widget.QMUIRadiusImageView2;
import com.ulook.planetmanager.R;
import com.ulook.planetmanager.base.WidgetData;
import com.ulook.planetmanager.common.AppServer;
import com.ulook.planetmanager.model.TUser;
import com.ulook.planetmanager.ui.home.user.UserDetailActivity;
import com.ulook.planetmanager.util.StringUtil;
import com.ulook.planetmanager.util.ThrowException;
import com.ulook.planetmanager.util.net.AsynRequestUtil;
import com.ulook.planetmanager.util.net.OkHttpException;
import com.ulook.planetmanager.util.net.ResponseCallback;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/11
 */
public class FriendView extends LinearLayout implements WidgetData<TUser> {

    private Context context;
    private TUser user;
    private int currUserId;

    public FriendView(Context context) {
        super(context);
        initFriendView(context);
    }

    public FriendView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initFriendView(context);
    }

    public FriendView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initFriendView(context);
    }

    private void initFriendView(Context context){
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.widget_friend_view, this, true);
    }


    @Override
    public void setWidgetData(TUser value) {
        this.user = value;
        QMUIRadiusImageView2 head = findViewById(R.id.w_friend_head_img);
        TextView name = findViewById(R.id.w_friend_name_tx);

        name.setText(user.getName());
        // 获取头像
        if(!StringUtil.isEmpty(user.getHeadimg())){
            ResponseCallback callback = new ResponseCallback() {
                @Override
                public void onSuccess(Object res) {
                    byte[] image = (byte[]) res;
                    Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                    head.setImageBitmap(bitmap);
                }
                @Override
                public void onFailure(OkHttpException failuer) {
                    ThrowException.print(failuer, failuer.getEmsg());
                }
            };
            AsynRequestUtil.loadImg(AppServer.HEAD_IMG_PATH + user.getHeadimg(), null, callback);
        }

        // 点击跳转用户页面
        this.setClickable(true);
        this.setOnClickListener(view -> {
            Intent intent = new Intent(context, UserDetailActivity.class);
            intent.putExtra("user", user);
            intent.putExtra("currUser", currUserId);
            context.startActivity(intent);
        });
    }

    @Override
    public void setWidgetData(TUser value, int currUserID) {
        this.currUserId = currUserID;
        setWidgetData(value);
    }
}
